﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.InvMovementServiceRef;
    using WMS6.Web.UserControls;

    public class OrderMovementList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F5222;
        protected GridView gvMovement;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2MovementList(MovementQueryEntity movementQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(movementQueryEntity.CurrentPage);
            DataSet ds = new DataSet();
            ds = InvMovementServiceClientProxy.GetInstance().GetMovementInfoByQueryEntity(movementQueryEntity);
            base.BindData2GridView(this.gvMovement, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvMovement, "MOVEMENT_ID");
                List<string> listStatus = base.GetSelectedIDsInGridView(this.gvMovement, "STATUS");
                if ((listStatus != null) || (listStatus.Count != 0))
                {
                    for (int i = 0; i < listStatus.Count; i++)
                    {
                        if (listStatus[i].ToString().ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F4100_011");
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
                }
                else
                {
                    this.lblMsg.Text = InvMovementServiceClientProxy.GetInstance().DeleteMovement(selectedID) + base.GetGlobalMsgResource("F4100_012");
                    MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
                    this.BindData2MovementList(movementQueryEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_013") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvMovement, "MOVEMENT_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_035");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_036");
            }
            else
            {
                base.Response.Redirect(string.Format("OrderMovement.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
            movementQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = movementQueryEntity.CurrentPage;
            movementQueryEntity.CurrentPage = "0";
            this.gvMovement.Columns[1].Visible = false;
            this.gvMovement.AllowSorting = false;
            this.BindData2MovementList(movementQueryEntity);
            this.hdnActionID.Value = "";
            this.gvMovement.ShowFooter = false;
            movementQueryEntity.IsGetAll = false;
            movementQueryEntity.CurrentPage = currentPageIndex;
            this.SetMovementQueryEntity(movementQueryEntity);
            this.hdnActionID.Value = "";
            base.Export2Excel("Movement.xls", this.gvMovement);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
            movementQueryEntity.CurrentPage = "0";
            this.BindData2MovementList(movementQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
            movementQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2MovementList(movementQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
            movementQueryEntity.MovementID = "";
            movementQueryEntity.MovementType = "";
            movementQueryEntity.ReasonCode = "";
            movementQueryEntity.Status = "";
            movementQueryEntity.MovementDate = "";
            this.BindData2MovementList(movementQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
            if (int.Parse(movementQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                movementQueryEntity.CurrentPage = (int.Parse(movementQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2MovementList(movementQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
            if (int.Parse(movementQueryEntity.CurrentPage) > 0)
            {
                movementQueryEntity.CurrentPage = (int.Parse(movementQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2MovementList(movementQueryEntity);
            }
        }

        protected void btnSearch_Click(object sernder, EventArgs e)
        {
            base.Server.Transfer("OrderMovementQuery.aspx", false);
        }

        private MovementQueryEntity GetMovementQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as MovementQueryEntity);
            }
            return (base.PageQueryEntitySession as MovementQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
                movementQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2MovementList(movementQueryEntity);
            }
        }

        protected void gvMovement_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5222", this.gvMovement);
        }

        protected void gvMovement_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvMovement_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.hdnRtnValue.Value = base.UrlReplace(this.gvMovement.DataKeys[e.NewSelectedIndex]["MOVEMENT_ID"].ToString());
            if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + this.hdnRtnValue.Value + "';  window.close();</script>", false);
            }
        }

        protected void gvMovement_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                MovementQueryEntity movementQueryEntity = this.GetMovementQueryEntity();
                movementQueryEntity.SortField = e.SortExpression;
                if (movementQueryEntity.SortDirection == "ASC")
                {
                    movementQueryEntity.SortDirection = "DESC";
                }
                else if (movementQueryEntity.SortDirection == "DESC")
                {
                    movementQueryEntity.SortDirection = "ASC";
                }
                this.BindData2MovementList(movementQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            MovementQueryEntity movementQueryEntity = new MovementQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    movementQueryEntity.MovementID = this.PreviousPage.MovementID;
                    movementQueryEntity.MovementType = this.PreviousPage.MovementType;
                    movementQueryEntity.ReasonCode = this.PreviousPage.ReasonCode;
                    movementQueryEntity.Status = this.PreviousPage.Status;
                    movementQueryEntity.MovementDate = this.PreviousPage.MovementDate;
                }
                else
                {
                    movementQueryEntity.MovementID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            movementQueryEntity.IsGetAll = false;
            movementQueryEntity.CurrentPage = "0";
            movementQueryEntity.PageSize = this.gvMovement.PageSize.ToString();
            movementQueryEntity.SortField = "MOVEMENT_ID";
            movementQueryEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvMovement.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvMovement.Columns[1].Visible = false;
                this.gvMovement.Columns[2].Visible = true;
                this.btnAction.Visible = false;
                this.btnDelete.Visible = false;
                this.btnEdit.Visible = false;
                this.btnExport.Visible = false;
                this.btnList.Visible = false;
                this.btnNew.Visible = false;
                this.btnRefresh.Visible = false;
                this.btnReport.Visible = false;
                this.btnReSearch.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetMovementQueryEntity(movementQueryEntity);
            this.BindData2MovementList(movementQueryEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetMovementQueryEntity(MovementQueryEntity movementQueryEntity)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = movementQueryEntity;
            }
            else
            {
                base.PageQueryEntitySession = movementQueryEntity;
            }
        }

        public OrderMovementQuery PreviousPage
        {
            get
            {
                return (OrderMovementQuery) base.PreviousPage;
            }
        }
    }
}

